//
//  YPCoreBaseTable_vc.swift
//  YPSwift
//
//  Created by ogawa on 2020/5/26.
//  Copyright © 2020 ogawa. All rights reserved.
//

import UIKit
import EmptyDataSet_Swift

class YPCoreBaseTable_vc: YPCoreBase_vc,UIScrollViewDelegate, EmptyDataSetSource{
    
    let headerRefreshTrigger = PublishSubject<Void>()
    let footerRefreshTrigger = PublishSubject<Void>()
    
    let isHeaderLoading = BehaviorRelay(value: false)
    let isFooterLoading = BehaviorRelay(value: false)
    
    lazy var tableView: YPCoreBaseTable_v = {
        let view = YPCoreBaseTable_v(frame: CGRect(), style: .plain)
//        view.emptyDataSetSource = self
//        view.emptyDataSetDelegate = self
        view.rx.setDelegate(self).disposed(by: rx.disposeBag)
        return view
    }()
    
    var clearsSelectionOnViewWillAppear = true

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if clearsSelectionOnViewWillAppear == true {
            deselectSelectedRow()
        }
    }
    
    /**
     界面基础设置
     */
    override func makeUI() {
        
//        stackView.spacing = 0
//        stackView.insertArrangedSubview(tableView, at: 0)
//
//        tableView.bindGlobalStyle(forHeadRefreshHandler: { [weak self] in
//           self?.headerRefreshTrigger.onNext(())
//        })
//
//        tableView.bindGlobalStyle(forFootRefreshHandler: { [weak self] in
//           self?.footerRefreshTrigger.onNext(())
//        })
//
//        isHeaderLoading.bind(to: tableView.headRefreshControl.rx.isAnimating).disposed(by: rx.disposeBag)
//        isFooterLoading.bind(to: tableView.footRefreshControl.rx.isAnimating).disposed(by: rx.disposeBag)
//
//        tableView.footRefreshControl.autoRefreshOnFoot = true
//
//        let updateEmptyDataSet = Observable.of(isLoading.mapToVoid().asObservable(), emptyDataSetImageTintColor.mapToVoid(), languageChanged.asObservable()).merge()
//        updateEmptyDataSet.subscribe(onNext: { [weak self] () in
//            self?.tableView.reloadEmptyDataSet()
//        }).disposed(by: rx.disposeBag)
//
//        error.subscribe(onNext: { [weak self] (error) in
//            var title = ""
//            var description = ""
//            let image = R.image.icon_toast_warning()
//            switch error {
//            case .serverError(let response):
//                title = response.message ?? ""
//                description = response.detail()
//            }
//            self?.tableView.makeToast(description, title: title, image: image)
//        }).disposed(by: rx.disposeBag)

    }
    
}

extension YPCoreBaseTable_vc {

    func deselectSelectedRow() {
        if let selectedIndexPaths = tableView.indexPathsForSelectedRows {
            selectedIndexPaths.forEach({ (indexPath) in
                tableView.deselectRow(at: indexPath, animated: false)
            })
        }
    }
}

extension YPCoreBaseTable_vc: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        if let view = view as? UITableViewHeaderFooterView {
//            view.textLabel?.font = UIFont(size: 15.0)!
            themeService.rx
                .bind({ $0.text }, to: view.textLabel!.rx.textColor)
                .bind({ $0.primaryDark }, to: view.contentView.rx.backgroundColor)
                .disposed(by: rx.disposeBag)
        }
    }
    
}
